﻿using MiniFox.Components;
using MiniFox.Data;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using MiniFox.Utility;
using Repository.Core.Models.Dto;
using Repository.Core.Models.Queries;
using Repository.Core.Models.VO;
using Repository.Core.Providers;
using Repository.Data.Entities.MySql;
using System.Data;
using System.Linq.Expressions;

namespace Repository.Data
{
    [AutoResolve]
    public class WorkSpaceProvider : EntityDataProvider<RepositoryContext>, IWorkSpaceProvider
    {
        private Expression<Func<WorkItem, WorkItemVo>> func = (item) => new WorkItemVo
        {
            Name = item.Name,
            Submitted = item.Submitted,
            UpdateTime = item.UpdateTime,
            Description = item.Description,
            Content = item.Content.ToJsonObject()
        };

        public ResultObject<IEnumerable<WorkItemVo>> Query(string repoName, string userId, PagedQueryObject<CommonQuery> request)
        {
            var query = (from item in this.CurrentDbContext.WorkItem where item.RepoName == repoName && item.OwnerId == userId select item);
            PagedResultSet<WorkItem> result = this.PagedQueryExecute<WorkItem>(query, request);
            ResultObject<IEnumerable<WorkItemVo>> response = result.GetResultObject(func);
            return response;
        }

        public WorkItemVo Item(string repoName, string userId, string name)
        {
            WorkItem item = this.CurrentDbContext.WorkItem.SingleOrDefault(w => w.RepoName == repoName && w.Name == name && w.OwnerId == userId);
            if (item == null)
                return null;
            return func.Compile()(item);
        }
        //protected WorkItemVo MapToVo(WorkItem item)
        //{
        //    return new WorkItemVo
        //    {
        //        Name = item.Name,
        //        Submitted = item.Submitted,
        //        UpdateTime = item.UpdateTime,
        //        Description = item.Description,
        //        Content = item.Content.ToJsonObject()
        //    };
        //}

        public bool Create(string repoName, WorkItemDto dto)
        {
            WorkItem item = new ();
            item.RepoName = repoName;
            item.Name = dto.Name;
            item.OwnerId = dto.OwnerID;
            item.OwnerName = dto.OwnerName;
            item.Submitted = dto.Submitted;
            item.Content = dto.Content.ToJsonString();
            item.UpdateTime = DateTime.Now;
            item.Description = dto.Description;

            return this.Insert(item) > 0;
        }

        public bool Update(string repoName, WorkItemDto dto)
        {
            WorkItem item = this.CurrentDbContext.WorkItem.SingleOrDefault(w => w.RepoName == repoName && w.Name == dto.Name && w.OwnerId == dto.OwnerID);
            item.Submitted = dto.Submitted;
            item.Description = dto.Description;
            item.Content = dto.Content.ToJsonString();
            item.UpdateTime = DateTime.Now;

            return this.Update(item) > 0;
        }

        public bool Delete(string repoName, string userId, string name)
        {
            WorkItem item = this.CurrentDbContext.WorkItem.SingleOrDefault(w => w.RepoName == repoName && w.Name == name && w.OwnerId == userId);
            return this.Delete(item) > 0;
        }
        
    }
}
